#include "stdafx.h"
#include "NxVehicleDesc.h"

void NxVehicleDesc::setToDefault()
{
	userData = NULL;
	motorDesc = NULL;
	gearDesc = NULL;
	transmissionEfficiency = 1.0f;
	differentialRatio = 1.0f;
	maxVelocity = 80;
	cameraDistance = 15.f;
	children.clear();
	//carWheels.clear();
}

bool NxVehicleDesc::isValid() const
{
	for (NxU32 i = 0; i < carWheels.size(); i++) 
	{
		if (!carWheels[i]->isValid())
		{
			cout<<"!carWheels[i]->isValid()"<<endl;
			return false;
		}

	}

	if (mass < 0)
	{
		cout<<"mass < 0"<<endl;
		return false;
	}

	return true;
}

void NxVehicleDesc::setCommonUsing
	( const NxVec3& pos, bool frontWheelDrive, bool backWheelDrive, bool corvette, bool monsterTruck, bool oldStyle )
{
	Cube_Desc* desc=new Cube_Desc();	
	desc->size=NxVec3(6,0.3,1);
	this->carShapes.push_back(desc);

	this->position				= pos;
	this->mass					= 2000;//monsterTruck ? 12000 : 
	this->digitalSteeringDelta	= 0.02f;
	this->steeringMaxAngle		= 20;
	this->motorForce			= 4000;//monsterTruck?180.f:
	this->maxVelocity			= 60.f;//(monsterTruck)?20.f:this->cameraDistance = 8.0f;

	NxVehicleMotorDesc* motorDesc = new NxVehicleMotorDesc();
	NxVehicleGearDesc* gearDesc = new NxVehicleGearDesc();
	NxReal wheelRadius = 0.4f;

	if(corvette)
	{
		this->maxVelocity = (monsterTruck)?40.f:80.f;
		motorDesc->setToCorvette();
		this->motorDesc = motorDesc;
		gearDesc->setToCorvette();
		this->gearDesc = gearDesc;
		this->differentialRatio = 3.42f;
		if(monsterTruck)	//*
		{
			this->differentialRatio*= 6.f;
			this->mass *= 2;
		}
		//this->differentialRatio = 5.f;
		wheelRadius = 0.33f;
		this->centerOfMass.set(0,-0.5f,0);
	}
	else
		this->centerOfMass.set(0,monsterTruck?-2.5f:2.5f,0);

	Wheel_Desc* wheelDesc[4];
	for(NxU32 i=0;i<4;i++)
	{
		wheelDesc[i] = new Wheel_Desc();
		//wheelDesc[i].wheelAxis.set(0,0,1);
		//wheelDesc[i].downAxis.set(0,-1,0);
		wheelDesc[i]->wheelApproximation = 10;
		//wheelDesc[i].wheelFlags |= NX_WF_BUILD_LOWER_HALF;
		wheelDesc[i]->wheelRadius = (monsterTruck)?wheelRadius*3.f:wheelRadius;
		wheelDesc[i]->wheelWidth = (monsterTruck)?0.3f:0.1f;
		wheelDesc[i]->wheelSuspension = (monsterTruck)?0.7f:0.2f;
		wheelDesc[i]->springRestitution = monsterTruck?(corvette?5000:4000):7000;
		wheelDesc[i]->springDamping = 180;
		wheelDesc[i]->springBias = 0.0f;
		wheelDesc[i]->maxBrakeForce = monsterTruck?1.2f:1.f;
		if (oldStyle)
		{
			wheelDesc[i]->frictionToFront = 0.1f;
			wheelDesc[i]->frictionToSide = corvette ? 0.1f : 0.99f;
		}
		else
		{
			wheelDesc[i]->wheelFlags |= NX_WF_USE_WHEELSHAPE;
		}
		this->carWheels.pushBack(wheelDesc[i]);
	}

	NxReal widthPos = (monsterTruck)?1.4f:1.09f;
	NxReal heightPos = -0.4f;	//(monsterTruck)?1.f:
	wheelDesc[0]->position.set( 1.3f, heightPos, widthPos-0.5f);
	wheelDesc[1]->position.set( 1.3f, heightPos,-widthPos+0.5f);
	wheelDesc[2]->position.set(-1.3f, heightPos, widthPos-0.5f);
	wheelDesc[3]->position.set(-1.3f, heightPos,-widthPos+0.5f);
	NxU32 flags = NX_WF_BUILD_LOWER_HALF;
	wheelDesc[0]->wheelFlags |= (frontWheelDrive?NX_WF_ACCELERATED:0) | NX_WF_STEERABLE_INPUT | flags;
	wheelDesc[1]->wheelFlags |= (frontWheelDrive?NX_WF_ACCELERATED:0) | NX_WF_STEERABLE_INPUT | flags;
	wheelDesc[2]->wheelFlags |= (backWheelDrive?NX_WF_ACCELERATED:0) | NX_WF_AFFECTED_BY_HANDBRAKE | flags;
	wheelDesc[3]->wheelFlags |= (backWheelDrive?NX_WF_ACCELERATED:0) | NX_WF_AFFECTED_BY_HANDBRAKE | flags;

	this->steeringSteerPoint.set(1.8, 0, 0);
	this->steeringTurnPoint.set(-1.5, 0, 0);
}